#ifndef FILMGRAIN_H
#define FILMGRAIN_H

#include "RenderEngine.h"
#include "RenderTarget.h"
#include "TextureLoader.h"

#include "Helper.h"
#include "GLError.h"

class FilmGrain
{
public:
	FilmGrain(float noise_intensity = 1.0f, float expose = 1.0f)
		: _noise_intensity(noise_intensity), _expose(expose)
	{

	}

	void init()
	{

	}

	void resize(int width, int height)
	{
	}

	static void static_init()
	{
		shader.init(IOS_PATH("shader", "Quad", "vert"), IOS_PATH("shader", "FilmGrain", "frag"));
		texture_noise = TextureLoader::CreateTexture3D(IOS_PATH("Texture", "Noise", "dds").c_str());
        
        check_gl_error();
	}

	void set_noise_intensity(float noise_intensity)
	{
		_noise_intensity = noise_intensity;
	}

	void set_expose(float expose)
	{
		_expose = expose;
	}

	void render(RenderTexture & src, RenderTexture & dst, float t)
	{
		shader.use();
		glViewport(0, 0, RenderContex::window_width, RenderContex::window_height);
		glm::vec2 pixel_size(1.0f / RenderContex::window_width, 1.0f / RenderContex::window_height);
		shader.bind_vec2("pixel_size", pixel_size);
		shader.bind_float("noise_intensity", _noise_intensity);
		shader.bind_float("exposure", _expose);
		shader.bind_float("t", t);
		shader.bind_texture("tex_src", src, 0);
		shader.bind_texture("tex_noise", texture_noise, 1, GL_TEXTURE_3D);

		RenderEngine::set_render_target(&dst, nullptr);
		ModelManager::screen_aligned_quad.render();
		RenderEngine::set_render_target(nullptr, nullptr);
	}

private:

	float _noise_intensity = 1.0f;
	float _expose = 1.0f;

	static Shader shader;
	static GLuint texture_noise;
};



#endif